package com.github.permissiondog.cancanpandroid.ui.view;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.github.permissiondog.cancanpandroid.R;
import com.github.permissiondog.cancanpandroid.repository.UserRepository;
import com.github.permissiondog.cancanpandroid.repository.impl.UserRepositoryImpl;

public class MainActivity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        UserRepositoryImpl.getInstance().currentUser(user -> user.ifPresentOrElse(u -> {
            switch (u.getRole()) {
                case ADMIN -> {
                    Toast.makeText(this, "管理员请使用网页登录", Toast.LENGTH_LONG).show();
                    startActivity(new Intent(this, LoginActivity.class));
                    finish();
                }
                case APPROVER -> {
                    Toast.makeText(this, "欢迎审批人 " + u.getNickname(), Toast.LENGTH_LONG).show();
                    startActivity(new Intent(this, ApproverMainActivity.class));
                    finish();
                }
                case APPLICANT -> {
                    Toast.makeText(this, "欢迎申请人 " + u.getNickname(), Toast.LENGTH_LONG).show();
                    startActivity(new Intent(this, ApplicantMainActivity.class));
                    finish();
                }
            }
        }, () -> {
            Toast.makeText(this, "请先登录", Toast.LENGTH_LONG).show();
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }));

    }
}